const { values } = require('lodash');
const MyPromise = require('./myPromise.js');

const promise = new MyPromise((resolve, reject) => {
  // 立即执行
  // resolve('success')
  reject('fail')
  // throw new Error('error')

  // setTimeout(() => {
  //   resolve('success')
  // }, 2000);
})

//3-1----------------------------------------
// promise.then(value => {
//   console.log(value)
// }, reason => {
//   console.log(reason)
// })

//3-3----------------------------------------
// promise.then(value => {
//   console.log(1)
//   console.log(value)
// }, reason => {
//   console.log(reason)
// })
// promise.then(value => {
//   console.log(2)
//   console.log(value)
// }, reason => {
//   console.log(reason)
// })
// promise.then(value => {
//   console.log(3)
//   console.log(value)
// }, reason => {
//   console.log(reason)
// })

//3-4&5-----------------------------------------
// function other() {
//   return new MyPromise((resolve, reject) => {
//     resolve('other')
//   })
// }
// promise.then(value => {
//   console.log(value)
//   return 100;
// }, reason => {
//   console.log(reason)
// }).then(value => {
//   console.log(value)
//   return other();
// }).then(value => {
//   console.log(value)
// })

//3-6------------------------------------------
// const p1 = new MyPromise((resolve, reject) => {
//   resolve(100)
// })

// var p2 = p1.then(value => {
//   console.log(value)
//   return p2;
// })

// p2.then(function () { }, function (reason) {
//   console.log(reason.message)
// })

// 3-7-----------------------------------------
// promise.then(value => {
//   return 'aaaa';
// }, reason => {
//   console.log(reason.message)
// }).then(value => {
//   console.log(value)
//   throw new Error('then message')
// }, reason => {
//   console.log(reason.message)
// }).then(value => {
//   console.log(value)
// }, reason => {
//   console.log(reason.message)
// })

//3-8-------------------------------------------
// promise.then().then().then().then(console.log, console.log);

//3-9-------------------------------------------
function p3() {
  return new MyPromise((resolve, reject) => {
    setTimeout(() => {
      resolve('p3')
    }, 2000);
  })
}

function p4() {
  return new MyPromise((resolve, reject) => {
    resolve('p4')
  })
}

function p5() {
  return new MyPromise((resolve, reject) => {
    reject('p5 fail')
  })
}

// MyPromise.all(['a', 'b', p3(), p4(), 1]).then(console.log)

//3-10-------------------------------------------
// MyPromise.resolve(100).then(console.log)
// MyPromise.resolve(promise).then(console.log)

//3-11-------------------------------------------

// promise.finally(() => {
//   console.log(`finally`)
//   return p3();
// }).then(value => console.log(value), reason => console.log(reason))

//3-12------------------------------------------
// promise.then(value => console.log(value)).catch(reason => console.log(reason));